Java megzine: Growing onopan by Michael Clarck & Manish Sahu
Author:Michael Clarck & Manish Sahu [Clarck, Michael]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2018-12-13T16:00:00+00:00
Download all listings in this issue as text The listener object can be reused if necessary, but if it’s used only once, the code in Listing 6 is considered good style.
In this example, the slightly odd syntax involving extra curly brackets is needed because action Performed returns void. We’ll see more about that later.
Let’s move on to take a look at the role that lambda expressions will play in writing modern code for handling collections, in particular the transition between two programming styles known as external and internal iteration.
External Versus Internal
Iteration
Up until now, the standard way of dealing with a Java collection is via external iteration. It’s called external iteration because there is control flow, external to the collection, which is used to iterate over the elements contained in the collection. This is the tradi
tional way of handling collections,
with which most Java program
mers are very familiar—even if
they don’t know or use the term.
For example, language con
structs such as the enhanced for
loop create an external iterator
and use that object to traverse the
collection shown in Listing 7.
With this approach, a collection
class represents a “monolithic”
view of all the elements in the col
lection, and the collection object
can provide random access to any
element of the collection that the
programmer might require.
In this view of the world, we
iterate by calling a method—
iterator()—on the collection
instance. This returns an objectblogof type Iterator, which is a more
restricted view on the same
collection. It does not expose
an interface allowing random
access; instead, it is designed35 for purely sequential handling of the contents of the collection. This sequential nature
also gives rise to the infamous ConcurrentModificationException when you try to use the collection with parallel access.
The alternative is to require the collection object to manage the iterator (and loop) internally. This approach is called internal iteration, and it is the preferred choice when working with lambda expressions.
In addition to new syntax for lambda expressions, Project Lambda includes a major upgrade to the collections libraries. The aim of the collections upgrade is to make it much easier to write code that uses internal iteration to support a range of well-known functional programming idioms.
Functional Idioms with
Lambdas
At one time or another, most developers have found themselves needing to perform one or more of the following operations on a collection:
■■ Create a new collection, which
filters out any elements that don’t satisfy some condition ■ ■ Apply a transformation to each element of a collection, and work with the transformed collection
■ ■ Build up an overall value for a property of the whole collection, for example, adding up or averaging the values
These tasks (which are called,
respectively, the filter, map, and reduce idioms) have something important in common: They all require code to be run on each element of the collection in turn.
Whether the code is testing an element to see whether a condition is met (filter), transforming the element into a new element for a new collection (map), or computing a value to be fed into an overall calculation (reduce), the key theme is “a bit of code that needs to be run on each element.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27095)
Hello! Python by Anthony Briggs(25950)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25286)
Kotlin in Action by Dmitry Jemerov(24396)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23313)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21945)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20849)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17073)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16833)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12584)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10651)
Hit Refresh by Satya Nadella(9239)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8446)
Robo-Advisor with Python by Aki Ranin(8390)